package com.example.medicalequipment.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.medicalequipment.R;
import com.example.medicalequipment.controller.UserController;
import com.example.medicalequipment.entity.User;
import com.example.medicalequipment.ui.activity.LoginActivity;

public class MyFragment extends Fragment {

    private TextView tvUsername, tvContact, tvUserType;
    private LinearLayout llLogout;
    private UserController userController;
    private int userId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);

        tvUsername = view.findViewById(R.id.tvUsername);
        tvContact = view.findViewById(R.id.tvContact);
        tvUserType = view.findViewById(R.id.tvUserType);
        llLogout = view.findViewById(R.id.llLogout);

        userController = new UserController(getActivity());

        // 获取用户ID
        userId = getActivity().getIntent().getIntExtra("userId", -1);

        // 加载用户信息
        loadUserInfo();

        // 设置退出登录点击事件
        llLogout.setOnClickListener(v -> logout());

        return view;
    }

    private void loadUserInfo() {
        userController.getUserById(userId, new UserController.GetUserCallback() {
            @Override
            public void onGetUserResult(User user) {
                if (user != null) {
                    tvUsername.setText("用户名: " + user.getUsername());
                    tvContact.setText("联系方式: " + user.getContact());

                    String userTypeText = user.isRepairUser() ? "维修人员" : "普通人员";
                    tvUserType.setText("用户类型: " + userTypeText);
                }
            }
        });
    }

    private void logout() {
        // 跳转到登录页面
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
        getActivity().finish();
    }
}